//
//  SettingViewController.m
//  HuateVideo
//
//  Created by  on 12-5-20.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SettingViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface SettingViewController ()

@end

@implementation SettingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)dealloc{
    [sw1 release];
    [sw2 release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor=TOOLBAR_TINTCOLOR;
    UITableView* table=[[UITableView alloc] initWithFrame:CGRectMake(10, 20, 300, 150)];
    table.layer.borderWidth=1;
    table.scrollEnabled=NO;
    table.opaque=NO;
    table.backgroundColor=[UIColor clearColor];
//    table.allowsSelection=NO;
    table.layer.borderColor=[LIGHT_COLOR CGColor];
    table.delegate=self;
    table.dataSource=self;
    table.separatorColor=LIGHT_COLOR;
    [self.view addSubview:table];
    [table release];

    UIView* v=[[UIView alloc] init];
    table.tableFooterView=v;
    [v release];
    
    sw1=[[RCSwitchClone alloc] initWithFrame:CGRectMake(0, 0, 80, 27)];
    sw1.tag = 1001;
    if ([[UserDefaults objectForKey:PLAY_SERIES_WHEN_WIFI] isEqualToString:@"YES"]) {
        sw1.on = YES;
    }
    else {
        sw1.on = NO;
    }
    
    [sw1 addTarget:self action:@selector(updateSetting:) forControlEvents:UIControlEventValueChanged];
    
    sw2 =[[RCSwitchClone alloc] initWithFrame:CGRectMake(0, 0, 80, 27)];
    sw2.tag = 1002;
    if ([[UserDefaults objectForKey:ALERT_WHEN_NET_CHANGE] isEqualToString:@"YES"]) {
        sw2.on = YES;
    }
    else {
        sw2.on = NO;
    }
    [sw2 addTarget:self action:@selector(updateSetting:) forControlEvents:UIControlEventValueChanged];
}

- (void)updateSetting:(RCSwitchClone *)sender
{
    if (sender.tag == 1001) {
        if (sw1.on) {
            [UserDefaults setObject:@"YES" forKey:PLAY_SERIES_WHEN_WIFI];
            
            [self performSelectorOnMainThread:@selector(showResult:) withObject:[NSNumber numberWithInt:1] waitUntilDone:NO];
        }
        else {
            [UserDefaults setObject:@"NO" forKey:PLAY_SERIES_WHEN_WIFI];
            [self performSelectorOnMainThread:@selector(showResult:) withObject:[NSNumber numberWithInt:2] waitUntilDone:NO];

        }
    }
    else if (sender.tag == 1002) {
        if (sw2.on) {
            [UserDefaults setObject:@"YES" forKey:ALERT_WHEN_NET_CHANGE];
            [self performSelectorOnMainThread:@selector(showResult:) withObject:[NSNumber numberWithInt:3] waitUntilDone:NO];

            
        }
        else {
            [UserDefaults setObject:@"NO" forKey:ALERT_WHEN_NET_CHANGE];
            
            [self performSelectorOnMainThread:@selector(showResult:) withObject:[NSNumber numberWithInt:4] waitUntilDone:NO];

        }
    }
}

- (void)showResult:(NSNumber *)index
{
    switch (index.intValue) {
        case 1:
        {
            UIAlertView	*alertview = [[UIAlertView alloc] initWithTitle:LS(@"提示")  
                                                                message:LS(@"已开启wifi下连续播放电视剧")
                                                               delegate:self 	
                                                      cancelButtonTitle:LS(@"确定")
                                                      otherButtonTitles:nil];		
            [alertview show];		
            [alertview release];
        }
            break;
        case 2:
        {
            UIAlertView	*alertview = [[UIAlertView alloc] initWithTitle:LS(@"提示")  
                                                                message:LS(@"已关闭wifi下连续播放电视剧")
                                                               delegate:self 	
                                                      cancelButtonTitle:LS(@"确定")
                                                      otherButtonTitles:nil];		
            [alertview show];		
            [alertview release];
        }
            break;
        case 3:
        {
            UIAlertView	*alertview = [[UIAlertView alloc] initWithTitle:LS(@"提示")  
                                                                message:LS(@"已开启网络切换时提醒")
                                                               delegate:self 	
                                                      cancelButtonTitle:LS(@"确定")
                                                      otherButtonTitles:nil];		
            [alertview show];		
            [alertview release];
        }
            break;
        case 4:
        {
            UIAlertView	*alertview = [[UIAlertView alloc] initWithTitle:LS(@"提示")  
                                                                message:LS(@"已关闭网络切换时提醒")
                                                               delegate:self 	
                                                      cancelButtonTitle:LS(@"确定")
                                                      otherButtonTitles:nil];		
            [alertview show];		
            [alertview release];
        }
            break;
            
        default:
            break;
    }
}

//-(void)changeSwith:(UISwitch*)sw{
//    UIImageView* v=(UIImageView*)[[[[sw subviews] lastObject] subviews] objectAtIndex:1];
////    v.image=[UIImage imageNamed:@"on.png"];
//    NSLog(@"%@",[v subviews]);
//
//}
//- (void)explode:(id)aView level:(int)aLevel {
//    for (int i = 0; i < aLevel; i++)
//        printf("-");
//    NSLog(@"%@",aView);
//    printf("%s:%s\n",[[[aView class] description] UTF8String],[[[aView superclass] description] UTF8String]);
//    
//    for(UIView *subview in [aView subviews])
//        [self explode:subview level:(aLevel + 1)];
//}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static   NSString* identify=@"cell";
    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:identify];
    if (cell==nil) {
        cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify] autorelease];
    }
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text=LS(@"开启wifi下连续播放电视剧");
            cell.accessoryView=sw1;
			cell.selectionStyle=UITableViewCellSelectionStyleNone;
            break;
        case 1:
            cell.textLabel.text=LS(@"开启网络切换时提醒");
            cell.accessoryView=sw2;
			cell.selectionStyle=UITableViewCellSelectionStyleNone;
            break;
        case 2:
            cell.textLabel.text=LS(@"清除缓存");
			cell.selectionStyle=UITableViewCellSelectionStyleGray;
            break;
        default:
            break;
    }
    cell.textLabel.textColor=[UIColor whiteColor];
    cell.textLabel.font=[UIFont systemFontOfSize:16];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	
    if (indexPath.row == 2) {
        [NSThread detachNewThreadSelector:@selector(clearAllCacheFromDiskAtThread) toTarget:self withObject:nil];
    }
    
}

- (void)clearAllCacheFromDiskAtThread{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
	
	AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [app clearAllCacheFromDisk];
	
	[self performSelectorOnMainThread: @selector(refreshCacheFooterView) withObject: nil waitUntilDone: YES];
	
	[pool release];
}


- (void)refreshCacheFooterView{
    UIAlertView	*alertview = [[UIAlertView alloc] initWithTitle:LS(@"提示")  
                                                        message:LS(@"清除缓存成功")
                                                       delegate:self 	
                                              cancelButtonTitle:LS(@"确定")
                                              otherButtonTitles:nil];		
    [alertview show];		
    [alertview release];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
